﻿using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;


namespace Settings
{
    public class SyncFolder : IComparable
    {
        public enum SyncType
        {
            ToSelectedFolder,
            ToSubFolderSyncDate,
            ToSubfolderCreationDate,
            ToSubfolderModifyDate
        };

        public enum CopyType
        {
            CopyOnlyNewFiles,
            CopyNewAndChangedFiles,
            CopyNewAndChangedFilesNoReplaceNotify
        };

        public enum CopyFormShowType
        {
            AlwaysShow,
            ShowOnlyWhenError,
            ShowWhenErrorAndComplete

        };
        
        public String Path;
        public ArrayList Masks = new ArrayList();
        public Boolean ProcessSubfolders;
        public SyncType SType;
        public String DestinationFolder;
        public CopyType CType;
        public CopyFormShowType CopyForm;

        public String FolderTemplate = @"Фотки_yyyy_MM_dd";

        public SyncFolder()
        {
          
    
        }

        public override String ToString()
        {
            return Path;
        }

        public int CompareTo(Object obj)
        {
            if (obj == null || GetType() != obj.GetType()) throw new ArgumentException("Invalid argument type!");
            SyncFolder p = (SyncFolder)obj;
            if (Path != null && p.Path != null) return Path.CompareTo(p.Path);
            return -1;
        }
    }
    
    public class MassStorageDevice : Object, IComparable
    {
        public string SerialNumber;
        public string Name;
        public bool   Ignore;
        public ArrayList folders = new ArrayList();

        [XmlIgnore]
        public String ActualDiskLetter;

        [XmlIgnore]
        public bool IsOnline;

        public override bool Equals(Object obj)
        {
            //Check for null and compare run-time types.
            Type T1 = GetType();
            Type T2 = obj.GetType();
            if (obj == null || GetType() != obj.GetType()) return false;
            MassStorageDevice p = (MassStorageDevice)obj;
            return (SerialNumber == p.SerialNumber);
        }

        public override int GetHashCode()
        {
            if (Name != null)
            {
                if (SerialNumber != null)
                {
                    return base.GetHashCode() ^ Name.GetHashCode() ^ SerialNumber.GetHashCode();
                }
                return base.GetHashCode() ^ Name.GetHashCode();
            }
            return base.GetHashCode();
        }

        public int CompareTo(Object obj)
        {
            if (obj == null || GetType() != obj.GetType()) throw new ArgumentException("Invalid argument type!");
            MassStorageDevice p = (MassStorageDevice)obj;
            if (SerialNumber != null && p.SerialNumber != null) return SerialNumber.CompareTo(p.SerialNumber);
            return -1;
        }

        public override String ToString()
        {
            String res = Name + "  (" + SerialNumber + ")";
            if (IsOnline) res = ActualDiskLetter + @": " + res;
            return res;
        }

    }

    
    public interface SettingsUpdater
    {
        void SettingWasUpdated();
    };



    [XmlRootAttribute("MASSBAckUp",
    IsNullable = false)]
    [XmlInclude(typeof(MassStorageDevice))]
    [XmlInclude(typeof(SyncFolder))]
    public class AppSettings
    {
        //private bool wasChanged = false;
        private SettingsUpdater _updater = null;
        private Boolean __Enable;
        public ArrayList devices = new ArrayList();


        public void Update()
        {
            if (_updater != null) _updater.SettingWasUpdated();
        }

        public AppSettings()
        {

        }

        public AppSettings(SettingsUpdater updater)
        {
            _updater = updater;
        }

        public void SetSettingsUpdater(SettingsUpdater updater)
        {
            _updater = updater;
        }

        

        public Boolean Enable
        {
            get { return __Enable; }
            set { __Enable = value; if (_updater != null) _updater.SettingWasUpdated(); }
        }
              

        public void Save(String FileName)
        {
            System.IO.TextWriter strm = new System.IO.StreamWriter(FileName);
            XmlSerializer xml = new XmlSerializer(typeof(AppSettings));
            xml.Serialize(strm, this);
            strm.Close();
        }

        public static AppSettings Load(String FileName, SettingsUpdater Updater)
        {
            AppSettings settings = null;
            System.IO.TextReader strm = new System.IO.StreamReader(FileName);
            XmlSerializer xml = new XmlSerializer(typeof(AppSettings));
            try
            {
                settings = (AppSettings)xml.Deserialize(strm);
            }
            catch (Exception /*Excp*/)
            {
            }
            strm.Close();
            settings.SetSettingsUpdater(Updater);
            settings.Update();
            return settings;
        }

    }

}
